<?php

declare(strict_types=1);

namespace app\shop\controller\admin\attr;

use app\operations\model\Store;
use think\exception\ValidateException;
use think\Request;
use sheep\controller\Backend;
use app\shop\model\attr\ShopType as ShopTypeModel;
use app\shop\model\attr\ShopTypeServiceProject as ShopTypeServiceProjectModel;
use \app\shop\validate\admin\attr\ShopType as ShopTypeValidate;
use think\Response;

/**
 * 门店服务属性管理
 */
class ShopType extends Backend
{
    public $organ_id;
    const PAGE = 1;
    const LIST_ROWS = 10;
    const ORDER = 'DESC';
    const SORT = 'id';
    private ShopTypeServiceProjectModel $service_model;

    public function initialize()
    {
        $this->organ_id = $this->shopId();
        $this->model = new ShopTypeModel();
        $this->service_model = new ShopTypeServiceProjectModel();
    }

    // 商家服务分类列表
    public function typelist()
    {
        $service = $this->model->serviceTypeInfo($this->organ_id);
        return success('获取成功', $service);
    }


    /**
     * 商家子服务列表
     *
     * @return Response
     */
    public function list(Request $request)
    {
        $params['page']      = $request->param('page', self::PAGE);
        $params['list_rows'] = $request->param('list_rows', self::LIST_ROWS);
        $params['search']    = json_decode($request->param('search', ''));
        $params['order']     = $request->param('order', self::ORDER);
        $params['sort']      = $request->param('sort', self::SORT);
        $params['id']        = $request->param('id', -1);
        $params['shop_id']   = $this->organ_id;

        $brands = $this->model->list($params);

        return success('获取成功', $brands);
    }




    /**
     * 商家服务添加
     *
     * @return Response
     */
    public function add(Request $request)
    {
        $brands =$request->param('types', []);
        try {
            validate(ShopTypeValidate::class)
                ->scene('add')
                ->check($brands);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return error($e->getError());
        }
        $brands['shop_id'] = $this->organ_id;
        $result = $this->model->dataCreate($brands);
        if (!empty($brands['service'])) {
            $brands['shop_type_id'] = $result;
            $this->service_model->serviceCreate($brands);
        }
        if (!$result) {
            return error('保存失败');
        }
        return success('保存成功');
    }


    /**
     * 商家子服务获取详情
     *
     * @param  $id
     * @return Response
     */
    public function detail($id)
    {
        if (empty($id)) {
            return error('参数不能为空');
        }
        $brand = $this->model->subServiceInfo($id);
        return success('获取成功', ['attrInfo' => $brand]);
    }



    /**
     * 门店服务修改属性
     *
     * @return Response
     */
    public function edit($id)
    {
        $brands =$this->request->param('types', []);
        try {
            validate(ShopTypeValidate::class)
                ->scene('edit')
                ->check($brands);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return error($e->getError());
        }
        $result = $this->model->editCreate($brands);
        if (!empty($brands['service'])) {
            $brands['shop_type_id'] = $brands['id'];
            $this->service_model->serviceCreate($brands);
        }
        if (!$result) {
            return error('更新失败');
        }
        return success('更新成功');
    }



    /**
     * 门店服务删除属性
     *
     * @param string $id 要删除的品牌
     * @return void
     */
    public function delete($id)
    {
        $id = explode(',', $id);
        if (empty($id)) {
            return error('参数不能为空');
        }
        $result = $this->model->delData($id);
        if (!$result) {
            return error('更新失败');
        }
        return success('删除成功');
    }

}
